package oj;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class LC56 {
    public int[][] merge(int[][] intervals) {
        Arrays.sort(intervals,new Comparator<int[]>(){
            @Override
            public int compare(int[] a,int[] b){
                return a[0]-b[0];
            }
        });
        List<int[]> list=new ArrayList<>();
        for(int[] interval:intervals){
            int L=interval[0];
            int R=interval[1];
            if(list.size()==0 || list.get(list.size()-1)[1]<L){
                list.add(interval);
            }else{
                list.add(new int[]{(Math.min(L,list.get(list.size()-1)[0])),(Math.max(R,list.get(list.size()-1)[1]))});
            }
        }
        int[][] arr=new int[list.size()][2];
        for(int i=0;i<list.size();i++){
            arr[i]=list.get(i);
        }
        return arr;
    }
}
